package com.action.pc.cash;

import com.bean.Cash_apply;
import com.bean.Cash_pay;
import com.bean.Member;
import com.bean.Sys_user;
import com.common.BaseAction;
import com.github.pagehelper.Page;
import com.service.cash.CashService;
import com.service.member.MemberService;
import com.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by lvjianqing on 2017/9/16.
 */
@Controller
@RequestMapping("cash")
public class CashAction extends BaseAction {
    Logger log = Logger.getLogger(CashAction.class);
    @Autowired
    private CashService cashService;
    @Autowired
    private MemberService memberService;

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request, ModelMap modelMap, CashVo vo) {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        HashMap<String, Object> paraMap = new HashMap<>();
        paraMap.put("memberid", vo.getMemberid());
        paraMap.put("paytype", vo.getPaytype());
        paraMap.put("status", vo.getStatus());
        paraMap.put("category", vo.getCategory());
        paraMap.put("cityid", vo.getCityid());
        paraMap.put("starttime", vo.getStarttime());
        paraMap.put("endtime", vo.getEndtime());
        Page page = cashService.findByPage(pageNum, pageSize, paraMap);
        modelMap.put("page", page);
        modelMap.put("pages", page.getPages());
        modelMap.put("pageNum", page.getPageNum());
        modelMap.put("pageSize", page.getPageSize());
        modelMap.put("vo", vo);
        return new ModelAndView("/pc/cash_list", modelMap);
    }

    @RequestMapping("pay")
    public ModelAndView pay(HttpServletRequest request, ModelMap modelMap, CashVo vo) {
        Integer applyId = vo.getId();
        Cash_pay pay = new Cash_pay();
        if (applyId != null) {
            pay = cashService.findCashPayByApplyId(applyId);
            Cash_apply apply = cashService.selectByPrimaryKey(applyId);
            Integer memberid = apply.getMemberid();
            Member member = memberService.selectByPrimaryKey(memberid);
            if(member!=null){
                modelMap.put("member", member);
            }else{
                modelMap.put("member",new Member());
            }
            modelMap.put("apply", apply);
        }
        modelMap.put("pay", pay);
        modelMap.put("applyId", applyId);
        return new ModelAndView("/pc/cash_pay", modelMap);
    }

    @RequestMapping("paysave")
    public ModelAndView paysave(HttpServletRequest request, ModelMap modelMap, CashVo vo) {
        Integer id = vo.getId();
        Byte status = vo.getStatus();
        Integer applyId = vo.getApplyId();
        String checkcontent = vo.getCheckcontent();
        Sys_user user = getSysUser();
        if (id != null && status != null && applyId != null) {
            Cash_apply apply = cashService.selectByPrimaryKey(applyId);
            if(apply.getStatus()==0){
                apply.setStatus(status);
                apply.setCheckcontent(checkcontent);
                apply.setCheckuserid(user.getId());
                if (status.equals((byte) 1)) {//审核通过
                    String serialno = vo.getSerialno();
                    Cash_pay pay = cashService.findCashPayByApplyId(applyId);
                    pay.setPayuserid(user.getId());
                    pay.setSerialno(serialno);
                    if(apply.getCategory()==(byte)1){
                        cashService.saveCashRebateVerify(apply, pay);
                    }else if(apply.getCategory()==(byte)2){
                        cashService.saveCashBalanceVerify(apply, pay);
                    }
                } else if (status.equals((byte) 2)) {//审核不通过
                    if(apply.getCategory()==(byte)1) {
                        cashService.saveCashRebateVerify(apply, null);
                    }else if(apply.getCategory()==(byte)2){
                        cashService.saveCashBalanceVerify(apply, null);
                    }
                }
            }
        }
        return new ModelAndView("redirect:/cash/list.htm", modelMap);
    }

    /**
     * 查看打款详情
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("detail")
    public ModelAndView detail(HttpServletRequest request, ModelMap modelMap, CashVo vo) {
        Integer applyId = vo.getId();
        Cash_pay pay = new Cash_pay();
        if (applyId != null) {
            pay = cashService.findCashPayByApplyId(applyId);
            Cash_apply apply = cashService.selectByPrimaryKey(applyId);
            Integer memberid = apply.getMemberid();
            Member member = memberService.selectByPrimaryKey(memberid);
            modelMap.put("member", member);
            modelMap.put("apply", apply);
        }
        modelMap.put("pay", pay);
        modelMap.put("applyId", applyId);
        return new ModelAndView("/pc/cash_detail", modelMap);
    }
}
